package com.nuanshui.heatedloan.fund.integration.pay.execute;

import com.nuanshui.heatedloan.fund.enums.PaymentBizType;
import com.nuanshui.heatedloan.fund.integration.pay.bean.PayConfirmHandlerMeta;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;


/**
 * 支付执行器注册类
 * @param 
 * @author guohao
 * @date 2017年1月10日 下午2:21:14
 * @since $
 */
public class PayExecuteRegistrar {
	
	private static final String PAY_BIZ_PREFIX = "pay_biz_";
	//注册的支付执行器
	private ConcurrentHashMap<String,PayExecute> payExecutes = new ConcurrentHashMap<>();

	private List<PayConfirmHandlerMeta> payConfirmHandlers = new ArrayList<>(); 
	
	private ConcurrentHashMap<String,PayConfirmHandler>   finalPayConfirmHandlers = new ConcurrentHashMap<>();
	
	
	
	
	public List<PayConfirmHandlerMeta> getPayConfirmHandlers() {
		return payConfirmHandlers;
	}


	public ConcurrentHashMap<String, PayConfirmHandler> getFinalPayConfirmHandlers() {
		return finalPayConfirmHandlers;
	}
	
	public PayConfirmHandler getFinalPayConfirmHandlers(String  code) {
		 PayConfirmHandler payConfirmHandler = this.finalPayConfirmHandlers.get(PAY_BIZ_PREFIX+code);
		 if(payConfirmHandler!=null) {
			 return payConfirmHandler;
		 }else {
			 return null;
		 }
		 
	}



	public PayExecute getPayExecute(PaymentBizType payBizType) {
		return this.getPayExecute(payBizType.getCode());	
	}
	
	public PayExecute getPayExecute(String code) {
		PayExecute  payExecute =  this.payExecutes.get(PAY_BIZ_PREFIX+code);
		
		if(payExecute!=null) {
			return payExecute;
		}else {//不支持的处理
			return null;
		}
	}
	
	
	public synchronized void registerFinalPayConfirmHandler(String code,PayConfirmHandler payConfirmHandler) {
		 String key = PAY_BIZ_PREFIX+code;
		 finalPayConfirmHandlers.put(key, payConfirmHandler);
		 Collections.sort(payConfirmHandlers);
	}
	
	
	public synchronized void registerPayConfirmHandler(PayConfirmHandlerMeta payConfirmHandler) {
		 payConfirmHandlers.add(payConfirmHandler);
		 Collections.sort(payConfirmHandlers);
	}
	
	/**
	 * 注册一个执行器
	 * @param @param key
	 * @param @param payExecute
	 * @author guohao
	 * @date 2017年1月10日 下午2:23:58
	 * @since $
	 */
	public void register(String code,PayExecute payExecute) {
		 String key = PAY_BIZ_PREFIX+code;
		 payExecutes.put(key, payExecute);	
	}

}
